<?php
/**
 * Nutzp is simple web mvc framework based on ioc.
 * @author Leo Ning 2010-09-08 http://www.usephp.cn/
 * @license GPL3
 */
@ob_start (); //将所有输出缓存到缓冲区(可以在此后的任何地方调用header函数而不用担心已经有输出内容)
//目录分隔符
defined ( 'DS' ) or define ( 'DS', DIRECTORY_SEPARATOR );
//默认nutzp.inc路径
defined ( 'NUTZP' ) or define ( 'NUTZP', dirname ( __FILE__ ) . DS );
//默认网站目录
defined ( 'WEBROOT' ) or define ( 'WEBROOT', NUTZP );
//DOCUMENT ROOT
defined ( 'DOCROOT' ) or define ( 'DOCROOT', WEBROOT . 'html' . DS );
//默认应用程序名称
defined ( 'WEBSITE' ) or define ( 'nutzp app' );
//应用程序目录名
defined ( 'APP' ) or define ( 'APP', 'apps' );
//默认应用程序目录
defined ( 'APPS' ) or define ( 'APPS', WEBROOT . APP . DS );
//布局目录
defined ( 'LAYOUT' ) or define ( 'LAYOUT', WEBROOT . 'layouts' . DS );
//当前IOC配置文件
defined ( 'CUR_IOC_CFG_FILE' ) or define ( "CUR_IOC_CFG_FILE", '' );
//默认配置文件目录
defined ( 'ETC' ) or define ( 'ETC', WEBROOT . 'etc' . DS );
//默认临时目录,要可读写
defined ( 'TMP' ) or define ( 'TMP', WEBROOT . 'tmp' . DS );
//======================以下为环境检测(代码来自WP)=====================
if (defined ( 'DEBUG' ) && DEBUG) {
	if (defined ( 'E_DEPRECATED' )) {
		error_reporting ( E_ALL & ~ E_DEPRECATED & ~ E_STRICT & ~ E_NOTICE );
	} else {
		error_reporting ( E_ALL & ~ E_NOTICE );
	}
	if (defined ( 'DEBUG_DISPLAY' ) && DEBUG_DISPLAY) {
		@ini_set ( 'display_errors', 1 );
	} else {
		@ini_set ( 'display_errors', 0 );
	}
	if (defined ( 'DEBUG_LOG' ) && DEBUG_LOG) {
		@ini_set ( 'log_errors', 1 );
		@ini_set ( 'error_log', TMP . 'debug.log' );
	}
} else {
	if (! defined ( 'DEBUG' )) {
		define ( 'DEBUG', false );
	}
	if (defined ( 'E_RECOVERABLE_ERROR' )) {
		error_reporting ( E_ERROR | E_WARNING | E_PARSE | E_USER_ERROR | E_USER_WARNING | E_RECOVERABLE_ERROR );
	} else {
		error_reporting ( E_ERROR | E_WARNING | E_PARSE | E_USER_ERROR | E_USER_WARNING );
	}
}
define ( 'FS_CHMOD_DIR', 0755 );
define ( 'FS_CHMOD_FILE', 0644 );
define ( 'FS_TIMEOUT', 30 );
@set_magic_quotes_runtime ( 0 );
@ini_set ( 'magic_quotes_sybase', 0 );
// 浏览器检测
$is_lynx = $is_gecko = $is_winIE = $is_macIE = $is_opera = $is_NS4 = $is_safari = $is_chrome = $is_iphone = $is_IE6 = $is_IE7 = $is_IE8 = false;

if (isset ( $_SERVER ['HTTP_USER_AGENT'] )) {
	if (strpos ( $_SERVER ['HTTP_USER_AGENT'], 'Lynx' ) !== false) {
		$is_lynx = true;
	} elseif (strpos ( strtolower ( $_SERVER ['HTTP_USER_AGENT'] ), 'chrome' ) !== false) {
		$is_chrome = true;
	} elseif (strpos ( strtolower ( $_SERVER ['HTTP_USER_AGENT'] ), 'safari' ) !== false) {
		$is_safari = true;
	} elseif (strpos ( $_SERVER ['HTTP_USER_AGENT'], 'Gecko' ) !== false) {
		$is_gecko = true;
	} elseif (strpos ( $_SERVER ['HTTP_USER_AGENT'], 'MSIE' ) !== false && strpos ( $_SERVER ['HTTP_USER_AGENT'], 'Win' ) !== false) {
		$is_winIE = true;
	} elseif (strpos ( $_SERVER ['HTTP_USER_AGENT'], 'MSIE' ) !== false && strpos ( $_SERVER ['HTTP_USER_AGENT'], 'Mac' ) !== false) {
		$is_macIE = true;
	} elseif (strpos ( $_SERVER ['HTTP_USER_AGENT'], 'Opera' ) !== false) {
		$is_opera = true;
	} elseif (strpos ( $_SERVER ['HTTP_USER_AGENT'], 'Nav' ) !== false && strpos ( $_SERVER ['HTTP_USER_AGENT'], 'Mozilla/4.' ) !== false) {
		$is_NS4 = true;
	}
}

if ($is_safari && strpos ( strtolower ( $_SERVER ['HTTP_USER_AGENT'] ), 'mobile' ) !== false) {
	$is_iphone = true;
}
$is_IE = ($is_macIE || $is_winIE);

if ($is_IE && preg_match ( '/MSIE\s+(([\d]+)\.[\d]+)/', $_SERVER ['HTTP_USER_AGENT'], $ver )) {
	$var = 'is_IE' . $ver [2];
	$$var = true;
	unset ( $ver );
	unset ( $var );
}
if (isset ( $_SERVER ['SERVER_SOFTWARE'] )) {
	$is_apache = (strpos ( $_SERVER ['SERVER_SOFTWARE'], 'Apache' ) !== false || strpos ( $_SERVER ['SERVER_SOFTWARE'], 'LiteSpeed' ) !== false);
	$is_IIS = (strpos ( $_SERVER ['SERVER_SOFTWARE'], 'Microsoft-IIS' ) !== false || strpos ( $_SERVER ['SERVER_SOFTWARE'], 'ExpressionDevServer' ) !== false);
	$is_iis7 = (strpos ( $_SERVER ['SERVER_SOFTWARE'], 'Microsoft-IIS/7.' ) !== false);
} else {
	$is_apache = $is_IIS = $is_iis7 = false;
}
if (! defined ( 'RUNTIME_MEMORY_LIMIT' )) {
	define ( 'RUNTIME_MEMORY_LIMIT', '32M' );
}
if (function_exists ( 'memory_get_usage' ) && (( int ) @ini_get ( 'memory_limit' ) < abs ( intval ( RUNTIME_MEMORY_LIMIT ) ))) {
	@ini_set ( 'memory_limit', RUNTIME_MEMORY_LIMIT );
}
if (function_exists ( 'mb_internal_encoding' )) {
	mb_internal_encoding ( 'UTF-8' );
}

function __unregister_GLOBALS() {
	if (! ini_get ( 'register_globals' )) {
		return;
	}
	if (isset ( $_REQUEST ['GLOBALS'] )) {
		die ( 'GLOBALS overwrite attempt detected' );
	}
	$noUnset = array ('GLOBALS', '_GET', '_POST', '_COOKIE', '_REQUEST', '_SERVER', '_ENV', '_FILES' );
	$input = array_merge ( $_GET, $_POST, $_COOKIE, $_SERVER, $_ENV, $_FILES, isset ( $_SESSION ) && is_array ( $_SESSION ) ? $_SESSION : array () );
	foreach ( $input as $k => $v ) {
		if (! in_array ( $k, $noUnset ) && isset ( $GLOBALS [$k] )) {
			$GLOBALS [$k] = NULL;
			unset ( $GLOBALS [$k] );
		}
	}
}
__unregister_GLOBALS ();
// Fix for IIS when running with PHP ISAPI
if (empty ( $_SERVER ['REQUEST_URI'] ) || (php_sapi_name () != 'cgi-fcgi' && preg_match ( '/^Microsoft-IIS\//', $_SERVER ['SERVER_SOFTWARE'] ))) {
	
	// IIS Mod-Rewrite
	if (isset ( $_SERVER ['HTTP_X_ORIGINAL_URL'] )) {
		$_SERVER ['REQUEST_URI'] = $_SERVER ['HTTP_X_ORIGINAL_URL'];
	} else if (isset ( $_SERVER ['HTTP_X_REWRITE_URL'] )) { // IIS Isapi_Rewrite
		$_SERVER ['REQUEST_URI'] = $_SERVER ['HTTP_X_REWRITE_URL'];
	} else {
		// Use ORIG_PATH_INFO if there is no PATH_INFO
		if (! isset ( $_SERVER ['PATH_INFO'] ) && isset ( $_SERVER ['ORIG_PATH_INFO'] )) {
			$_SERVER ['PATH_INFO'] = $_SERVER ['ORIG_PATH_INFO'];
		}
		// Some IIS + PHP configurations puts the script-name in the path-info (No need to append it twice)
		if (isset ( $_SERVER ['PATH_INFO'] )) {
			if ($_SERVER ['PATH_INFO'] == $_SERVER ['SCRIPT_NAME'])
				$_SERVER ['REQUEST_URI'] = $_SERVER ['PATH_INFO'];
			else
				$_SERVER ['REQUEST_URI'] = $_SERVER ['SCRIPT_NAME'] . $_SERVER ['PATH_INFO'];
		}
		
		// Append the query string if it exists and isn't null
		if (isset ( $_SERVER ['QUERY_STRING'] ) && ! empty ( $_SERVER ['QUERY_STRING'] )) {
			$_SERVER ['REQUEST_URI'] .= '?' . $_SERVER ['QUERY_STRING'];
		}
	}
}

// Fix for PHP as CGI hosts that set SCRIPT_FILENAME to something ending in php.cgi for all requests
if (isset ( $_SERVER ['SCRIPT_FILENAME'] ) && (strpos ( $_SERVER ['SCRIPT_FILENAME'], 'php.cgi' ) == strlen ( $_SERVER ['SCRIPT_FILENAME'] ) - 7)) {
	$_SERVER ['SCRIPT_FILENAME'] = $_SERVER ['PATH_TRANSLATED'];
}
// Fix for Dreamhost and other PHP as CGI hosts
if (strpos ( $_SERVER ['SCRIPT_NAME'], 'php.cgi' ) !== false) {
	unset ( $_SERVER ['PATH_INFO'] );
}
// Fix empty PHP_SELF
$PHP_SELF = $_SERVER ['PHP_SELF'];
if (empty ( $PHP_SELF )) {
	$_SERVER ['PHP_SELF'] = $PHP_SELF = preg_replace ( "/(\?.*)?$/", '', $_SERVER ["REQUEST_URI"] );
}
if (version_compare ( '5.2', phpversion (), '>' )) {
	die ( sprintf ( 'Your php verion is %s,but nutzp reqired  php 5.2+', phpversion () ) );
}

/**
 * 加载文件
 * @param mixed $files 加载的单个文件或文件列表,文件路径为相对NUTZP或WEBROOT,如:<br/>
 * ioc/Ioc.inc
 */
function uses($files) {
	static $_files = null;
	$files = is_array ( $files ) ? $files : array ($files );
	if (is_null ( $_files )) {
		$_files = array ();
	}
	foreach ( $files as $file ) {
		if (empty ( $file )) {
			continue;
		}
		$filex = str_replace ( '/', DS, $file );
		if (! preg_match ( '/.*\.(inc|php|html)$/i', $file )) {
			$filex = str_replace ( '.', DS, $filex ) . '.inc';
		}
		if (in_array ( md5 ( $filex ), $_files )) {
			continue;
		}
		if (is_file ( NUTZP . $filex )) {
			include NUTZP . $filex;
			$_files [] = md5 ( $filex );
		} else if (is_file ( WEBROOT . $filex )) {
			include WEBROOT . $filex;
			$_files [] = md5 ( $filex );
		} else if (is_file ( APPS . $filex )) {
			include $filex;
			$_files [] = md5 ( $filex );
		} else if (is_file ( $filex )) {
			include $filex;
			$_files [] = md5 ( $filex );
		} else {
			trigger_error ( '[' . WEBSITE . "] The file {$file} was not found", E_USER_WARNING );
		}
	}
}
/**
 * 得到应用程序信息
 * @param string $app 应用程序,为空时扫描APPS目录下所有应用
 * @param boolean $reverse 当前$app为空时是否递归扫描APPS目录下所有应用
 * @return AppMeta 如果$app为空返回所有应用程序信息(数组),反之应用程序信息
 */
function get_appmeta($app = '', $reverse = true) {
	$appinfos = array ();
	$single = false;
	if (empty ( $app ) || ! is_dir ( APPS . $app )) {
		$appinfos = find_files ( APPS, '/^appinfo\.xml$/', array ('actions', 'views', 'plugins' ), true );
	} else if (is_dir ( APPS . $app )) {
		$appinfos = find_files ( APPS . $app, '/^appinfo\.xml$/', array ('actions', 'views', 'plugins' ) );
		$single = true;
	}
	if (empty ( $appinfos )) {
		return null;
	}
	$metas = array ();
	foreach ( $appinfos as $cfg ) {
		$xml = new XML ();
		$info = $xml->parse ( file_get_contents ( $cfg ) );
		$xml->destruct ();
		$xml = null;
		if (! is_null ( $info )) {
			$metas [] = new AppMeta ( $info );
		}
	}
	return $single ? @array_shift ( $metas ) : $metas;
}
/**
 * 得到当前IOC容器,因为每个应用可以自行设置IOC配置文件,所以IOC容器会有所不同.
 * @return Ioc
 */
function get_current_ioc() {
	if (defined ( 'CUR_IOC_CFG_FILE' )) {
		$ioc = Ioc::getIoc ( CUR_IOC_CFG_FILE );
	} else {
		$ioc = Ioc::getIoc ();
	}
	return $ioc;
}
/**
 * 加载插件
 */
function load_plugins() {
	$plugins = find_files ( APPS, '/.*_?plugin\.inc$/', array ('actions', 'views' ), true );
	if (! empty ( $plugins )) {
		uses ( $plugins );
	}
}
//会话与缓存实例
$__sess = $__cache = $__rbac = null;
//加载内核依赖文件
uses ( array ('utility.functions', 'utility.formatting', 'utility.plugin', 'libs.I18n', 'ioc.Ioc', 'libs.ICache', 'libs.IRequest', 'libs.ISession', 'libs.Response', 'libs.IRbac', 'mvc.mvc', 'libs.AppMeta', 'libs.Nutzp' ) );
//加载IOC容器
$_IOC = Ioc::getIoc ( CUR_IOC_CFG_FILE );
$_REQ = $_IOC->get ( 'request' );
$locale = $_REQ->cookie ( 'ntz_locale', 'zh_CN' );
//加载语言
load_core_textdomain ();
load_default_textdomain ();
//加载插件
if (defined ( 'USE_PLUGIN' ) && USE_PLUGIN) {
	load_plugins ();
}
//end of nutzp.inc file